window.onload = function (){
    $.get({
        url: "alltype",
        type: "get",
        dataType:"json",
        success:function (data){
            let result = "";
            for (let tr of data){
                result = "<option value='"+tr.tid+"'>"+tr.tname+"</option>";
                $("#type").append(result);
            }
        }
    })
    $.get({
        url:"alltown",
        type:"get",
        dataType:"json",
        success:function (data){
            let result = "";
            for (let tr of data){
                result = "<option value='"+tr.townid+"'>"+tr.townname+"</option>";
                $("#town").append(result);
            }
        }
    })
    $.get({
        url:"allstreet",
        type:"get",
        dataType:"json",
        success:function (data){
            let result = "";
            for (let tr of data){
                result = "<option value='"+tr.sid+"'>"+tr.sname+"</option>";
                $("#street").append(result);
            }
        }
    })
}

$("#buttonsub").click(function () {
    var title = $("#title").val();
    if (title == ""){
        $("#titmsg").html("标题不能为空");
        return;
    }else{
        $("#titmsg").html("");
    }
    var area = $("#area").val();
    if (area == ""){
        $("#areamsg").html("面积不能为空");
        return;
    }else{
        $("#areamsg").html("");
    }
    var price = $("#price").val();
    if (price == ""){
        $("#pricemsg").html("价格不能为空");
        return;
    }else{
        $("#pricemsg").html("");
    }
    var date = $("#date").val();
    if (date == ""){
        $("#datemsg").html("日期不能为空");
        return;
    }else{
        $("#datemsg").html("");
    }
    var phone = $("#phone").val();
    if (phone == ""){
        $("#phonemsg").html("联系方式不能为空");
        return;
    }else{
        $("#phonemsg").html("");
    }
    var context = $("#context").val();
    if (context == ""){
        $("#contextmsg").html("简介不能为空");
        return;
    }else{
        $("#contextmsg").html("");
    }
    var a = $("#type option:selected");
    var town = $("#town option:selected");
    var street = $("#street option:selected");
    $.post({
        url: "addHouse",
        data: {
            "houseEntity.title": $("#title").val(),
            "houseEntity.type": a.val(),
            "houseEntity.area": $("#area").val(),
            "houseEntity.price": $("#price").val(),
            "houseEntity.date": $("#date").val(),
            "houseEntity.town": town.val(),
            "houseEntity.street": street.val(),
            "houseEntity.phone": $("#phone").val(),
            "houseEntity.context": $("#context").val()
        },
        type: "post",
        success:
            function (data) {
                alert(data.msg);
                document.location = "guanli.htm";
            }
    })
})